package cc.orange.http;

/**
 * Created by CM on 2021/3/1.
 */

import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.CallAdapterFactory;
import com.xcheng.retrofit.RetrofitFactory;

import java.security.cert.CertificateException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import cc.orange.entity.BaseEntity;
import cc.orange.entity.BaseStrEntity;
import cc.orange.entity.BlockListEntity;
import cc.orange.entity.ChatListEntity;
import cc.orange.entity.ConveListEntity;
import cc.orange.entity.KFChatListEntity;
import cc.orange.entity.LoginsEntity;
import cc.orange.entity.MoABEntity;
import cc.orange.entity.NoteExtrEntity;
import cc.orange.entity.SuerInfosEntity;
import cc.orange.utils.NetConstant;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PartMap;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiService {
    String HOST = "http://60.205.204.159:8085";
//    String HOST = "http://192.168.31.192:8084";
//String HOST = "http://test.freorange.com";

   static void init() {

        DownLoadInterceptor interceptor = new DownLoadInterceptor();
        interceptor.setLevel(DownLoadInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HOST)
                .client(getUnsafeOkHttpClient())
                .callFactory(new OkHttpClient.Builder()
                        .addNetworkInterceptor(interceptor)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .callTimeout(60,TimeUnit.SECONDS)
                        .readTimeout(60,TimeUnit.SECONDS)
                        .writeTimeout(60,TimeUnit.SECONDS)
                        .build())
                //必须添加此adapter 用于构建Call
                .addCallAdapterFactory(CallAdapterFactory.INSTANCE)
                //添加自定义json解析器
                .addConverterFactory(MyGsonConverterFactory.create())
                .build();
        RetrofitFactory.DEFAULT = retrofit;
    }

    public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
                    .connectTimeout(NetConstant.API_CONNECT_TIME_OUT, TimeUnit.SECONDS)
                    .readTimeout(NetConstant.API_READ_TIME_OUT, TimeUnit.SECONDS)
                    .callTimeout(NetConstant.API_READ_TIME_OUT, TimeUnit.SECONDS)
                    .writeTimeout(NetConstant.API_WRITE_TIME_OUT, TimeUnit.SECONDS);
            okHttpBuilder.sslSocketFactory(sslSocketFactory);
            okHttpBuilder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            return okHttpBuilder.build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /*

     */
    @POST(HOST + "/api/login")
    Call<LoginsEntity> login(@Body RequestBody request);

    @POST(HOST + "/api/login/userLogin")
    Call<LoginsEntity> userLogin(@Body RequestBody request);


    @POST(HOST + "/api/user/updateUser")
    Call<LoginsEntity> updateUser(@Header ("Authorization")String heads,@Body RequestBody request);


    //    用户关注接口
    @GET(HOST + "/api/default/attention/{uId}/{flag}")
    Call<BaseEntity> attention(@Header ("Authorization")String heads,
                               @Path("uId") String uId,
                               @Path("flag") String flag);

    //    用户评价接口
    @GET(HOST + "/api/default/evaluate/{dId}/{uId}/{flag}")
    Call<BaseEntity> evaluate(@Header ("Authorization")String heads,
                                            @Path("dId") String dId,
                                             @Path("uId") String uId,
                                             @Path("flag") String flag);

    //    拉黑 block按钮
    @GET(HOST + "/api/default/block/{uId}/{flag}")
    Call<BaseEntity> block(@Header ("Authorization")String heads,
                               @Path("uId") String uId,
                               @Path("flag") String flag);

    // 不看Ta的动态 noWatch 按钮
    @GET(HOST + "/api/default/noWatch/{uId}/{flag}")
    Call<BaseEntity> noWatch(@Header ("Authorization")String heads,
                             @Path("uId") String uId,
                             @Path("flag") String flag);


//    // 保存聊天记录
    @POST(HOST + "/api/chat/saveChat")
    Call<BaseEntity> saveChat(@Header ("Authorization")String heads,
                              @Body RequestBody request);


    // 视频点赞
    @GET(HOST + "/api/default/likeVideo/{videoId}/{uId}/{flag}")
    Call<BaseEntity> likeVideo(@Header ("Authorization")String heads,
                               @Path("videoId") String videoId,
                               @Path("uId") String uId,
                               @Path("flag") String flag);


    // 举报
    @POST(HOST + "/api/default/report")
    Call<BaseEntity> report(@Header ("Authorization")String heads,
                            @Body RequestBody request);


    // 用户发送客服信息
    @POST(HOST + "/api/chat/returnKefu")
    Call<BaseEntity> returnKefu(@Header ("Authorization")String heads,
                                @Body RequestBody request);


    // 获取客服聊天记录
    @GET(HOST + "/api/chat/getKeFuChat")
    Call<KFChatListEntity> getKeFuChat(@Header ("Authorization")String heads);

    // 公告接口
    @GET(HOST + "/api/user/deleteUser")
    Call<BaseEntity> deleteUser(@Header("Authorization") String heads);

    // 发布内容
    @POST(HOST + "/api/user/insertDynamic")
    Call<BaseEntity> insertDynamic(@Header ("Authorization")String heads,
                            @Body RequestBody request);


    // 查询用户拉黑列表
    @GET(HOST + "/api/block/list")
    Call<BlockListEntity> blockList(@Header ("Authorization")String heads);

    //  删除拉黑的用户
    @POST(HOST + "/api/block/remove")
    Call<BlockListEntity> blockRemove(@Header ("Authorization")String heads, @Body RequestBody request);



    // 修改当前登录人的密码
    @POST(HOST + "/api/user/updatePass")
    Call<BaseEntity> updatePass(@Header ("Authorization")String heads, @Body RequestBody request);


    // 获取当前登录人信息
    @GET(HOST + "/api/user/getSuserInfo")
    Call<SuerInfosEntity> getSuserInfo(@Header ("Authorization")String heads);


    // 用户塞入纸条
    @POST(HOST + "/api/note/add")
    Call<BaseEntity> NoteAdd(@Header ("Authorization")String heads, @Body RequestBody request);


    // 用户抽取纸条
    @POST(HOST + "/api/note/extract")
    Call<NoteExtrEntity> NoteExtract(@Header ("Authorization")String heads, @Body RequestBody request);


    // 获取消息列表 悄悄话
    @GET(HOST + "/api/chat/chatList")
    Call<ChatListEntity> chatList(@Header ("Authorization")String heads,
                                  @Query("type") String type);

    // 收一条悄悄话 悄悄话
    @GET(HOST + "/api/hole/collect")
    Call<ChatListEntity> collect(@Header ("Authorization")String heads);

    // 发送悄悄话 悄悄话
    @POST(HOST + "/api/hole/add")
    Call<ChatListEntity> holeAdd(@Header ("Authorization")String heads, @Body RequestBody request);

    // 清空聊天记录
    @POST(HOST + "/api/chat/remove")
    Call<BaseEntity> chatRemove(@Header ("Authorization")String heads, @Body RequestBody request);

    // 新增投诉记录
    @POST(HOST + "/api/complaint/add")
    Call<BaseEntity> complaintAdd(@Header ("Authorization")String heads, @Body RequestBody request);

    // 拉黑用户
    @POST(HOST + "/api/block/add")
    Call<BaseEntity> blockAdd(@Header ("Authorization")String heads, @Body RequestBody request);


    // 添加修改备注
    @POST(HOST + "/api/remarks/addAndEdit")
    Call<BaseEntity> addAndEdit(@Header ("Authorization")String heads, @Body RequestBody request);

    // 切换当前登录人分身状态
    @POST(HOST + "/api/user/updateSpareStatus")
    Call<BaseEntity> updateSpareStatus(@Header ("Authorization")String heads, @Body RequestBody request);

    // 获取消息记录
    @POST(HOST + "/api/chat/findChat")
    Call<ConveListEntity> findChat(@Header ("Authorization")String heads, @Body RequestBody request);

    @Multipart
    @POST(HOST + "/api/load/upload")
    Call<BaseStrEntity> upload(@Header ("Authorization")String heads, @PartMap Map<String, RequestBody> params);

    // 获取其他用户的信息
    @GET(HOST + "/api/user/getUserDetail")
    Call<NoteExtrEntity> getUserDetail(@Header ("Authorization")String heads,
                                        @Query("userId") String userId,
                                        @Query("spareStatus") String spareStatus);

    //
    @POST(HOST + "/api/note/reply")
    Call<BaseEntity> reply(@Header ("Authorization")String heads, @Body RequestBody request);



    //appName   pkgName
    @POST(HOST + "/api/getIpAddress/detail")
    Call<MoABEntity> getIpAddresDetail(@Header("Authorization") String heads,
                                       @Query("codeName") String codeName,
                                       @Query("appName") String appName,
                                       @Query("pkgName") String pkgName);

}



